import pygame
import sys
import random
import time

# 初始化 Pygame
pygame.init()

# 设置窗口大小
WINDOW_WIDTH = 800
WINDOW_HEIGHT = 600

# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)

# 创建窗口
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("超级玛丽")

# 定义游戏角色
class Player(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()
        self.image = pygame.Surface((50, 50))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT - 100)
        self.speed = 5
        self.jump_speed = 10
        self.gravity = 0.5
        self.is_jumping = False
        self.lives = 3

    def update(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.rect.x -= self.speed
        if keys[pygame.K_RIGHT]:
            self.rect.x += self.speed
        if keys[pygame.K_UP] and not self.is_jumping:
            self.is_jumping = True
            self.rect.y -= self.jump_speed

        # 应用重力
        self.rect.y += self.gravity

        # 限制角色在窗口内
        if self.rect.left < 0:
            self.rect.left = 0
        if self.rect.right > WINDOW_WIDTH:
            self.rect.right = WINDOW_WIDTH
        if self.rect.bottom > WINDOW_HEIGHT:
            self.rect.bottom = WINDOW_HEIGHT
            self.is_jumping = False

    def draw(self, surface):
        surface.blit(self.image, self.rect)

# 定义障碍物
class Obstacle(pygame.sprite.Sprite):
    def __init__(self, x, y):
        super().__init__()
        self.image = pygame.Surface((50, 50))
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)

    def update(self):
        self.rect.x -= 5
        if self.rect.right < 0:
            self.kill()

    def draw(self, surface):
        surface.blit(self.image, self.rect)

# 创建角色和障碍物组
all_sprites = pygame.sprite.Group()
obstacles = pygame.sprite.Group()

player = Player()
all_sprites.add(player)

# 游戏循环
clock = pygame.time.Clock()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            # 打印"Game Over"到窗体上
            font = pygame.font.Font(None, 72)  # 增大字体
            text = font.render("Game Over", True, RED)  # 改为红色
            window.blit(text, (WINDOW_WIDTH // 2 - 100, WINDOW_HEIGHT // 2 - 36))
            pygame.display.flip()
            time.sleep(3)
            pygame.quit()
            sys.exit()

    # 生成障碍物
    if random.randint(0, 100) < 5:
        obstacle = Obstacle(WINDOW_WIDTH, random.randint(50, WINDOW_HEIGHT - 50))
        all_sprites.add(obstacle)
        obstacles.add(obstacle)

    # 更新和绘制所有精灵
    all_sprites.update()
    window.fill(BLACK)
    all_sprites.draw(window)

    # 检查碰撞
    hits = pygame.sprite.spritecollide(player, obstacles, False)
    if hits:
        player.lives -= 1
        if player.lives == 0:
            # 打印"Game Over"到窗体上
            font = pygame.font.Font(None, 72)  # 增大字体
            text = font.render("Game Over", True, RED)  # 改为红色
            window.blit(text, (WINDOW_WIDTH // 2 - 100, WINDOW_HEIGHT // 2 - 36))
            pygame.display.flip()
            time.sleep(3)
            pygame.quit()
            sys.exit()
        else:
            for obstacle in hits:
                obstacle.kill()

    # 显示生命值
    font = pygame.font.Font(None, 36)
    text = font.render(f"Lives: {player.lives}", True, WHITE)
    window.blit(text, (10, 10))

    # 更新屏幕
    pygame.display.flip()
    clock.tick(60)

